<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Global Mall</title>
    <link rel="icon" href="assets/image/logo/favicon.png" type="image/png" sizes="32x32">
    <link href="assets/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="assets/css/owl.carousel.min.css">
    <link rel="stylesheet" type="text/css" href="assets/css/owl.theme.default.min.css">
    <link rel="stylesheet" type="text/css" href="assets/fonts/css2.css">
    <link rel="stylesheet" type="text/css" href="assets/css/jquery.fancybox.min.css">
    <link rel="stylesheet" type="text/css" href="assets/css/all.min.css">
    <link rel="stylesheet" type="text/css" href="assets/css/style.css">
    <link rel="stylesheet" type="text/css" href="assets/css/responsive.css">
</head>

<body>
    <div class="preloader"></div>
    <?php
    // 引入数据库连接
    include 'conn.php';
    include 'header.php';
    if (!isset($_SESSION['user_id'])) {
        echo "<script>alert('请先登录！');window.location.href='login.php';</script>";
        exit;
    }
    ?>
    <!-- login page -->
    <div class="mb-3 mt-3">
        <div class="container">
            <div class="row">
                <div class="col-12">
                    <ul class="breadcums">
                        <li class="d-inline-block font-weight-bolder button-group2">
                            <a href="index.php" class="login_txt symbol2 pro_home"></a>
                        </li>
                        <li class="d-inline-block hr_ font-weight-bolder"><a href="#" class="login_txt">资 讯 详 情</a></li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
    <?php
    // 获取新闻ID
    if (isset($_GET['id'])) {
        $news_id = intval($_GET['id']);  // 确保是整数型的ID
        // 从数据库中查询该新闻的详情
        $sql = "SELECT * FROM news WHERE id = $news_id";
        $result = $conn->query($sql);
        // 如果查询有结果
        if ($result->num_rows > 0) {
            $news = $result->fetch_assoc();  // 获取新闻数据
        } else {
            // 如果没有找到该新闻，跳转到新闻列表页
            header("Location: news.php");
            exit();
        }
    } else {
        // 如果没有传入ID，跳转到新闻列表页
        header("Location: news.php");
        exit();
    }
    // 更新新闻的浏览量（如果需要）
    $view_count = $news['view_count'] + 1;
    $update_views_sql = "UPDATE news SET view_count = $view_count WHERE id = $news_id";
    $conn->query($update_views_sql);
    ?>
    <div class="news-detail-container">
        <!-- 新闻标题 -->
        <div class="news-header">
            <h1 class="news-title"><?php echo htmlspecialchars($news['title']); ?></h1>
            <p class="news-meta">
                发布于 <span><?php echo date('d M, Y', strtotime($news['created_at'])); ?></span> | 浏览量:
                <span class="views-count"><?php echo $news['view_count']; ?></span>
            </p>
        </div>
        <!-- 新闻图片 -->
        <?php if ($news['image']): ?>
            <div class="news-image">
                <img src="<?php echo htmlspecialchars($news['image']); ?>" alt="新闻图片" class="img-fluid">
            </div>
        <?php endif; ?>
        <!-- 新闻内容 -->
        <div class="news-content">
            <p><?php echo nl2br(htmlspecialchars($news['content'])); ?></p>
        </div>

        <!-- 返回按钮 -->
        <a href="news.php" class="back-btn">返回新闻列表</a>
    </div>
    <?php
    include 'footer.php';
    ?>
    </script>
    <script src="assets/js/jquery-3.4.1.min.js"></script>
    <script src="assets/js/owl.carousel.js"></script>
    <script src="assets/js/popper.min.js"></script>
    <script src="assets/js/bootstrap.min.js"></script>
    <script src="assets/js/jquery.fancybox.min.js"></script>
    <script src="assets/js/custom.js"></script>
    <script src="https://unpkg.com/aos@2.3.1/dist/aos.js"></script>
</body>

</html>